﻿using System;
using System.Collections.Generic;
using System.Reflection;
using System.Resources;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using Converter;
using HeThongThongTin.Library;
using Telerik.Windows.Controls;
using Telerik.Windows.Controls.GridView;
using System.ComponentModel;
using Utility;

namespace HeThongThongTin.UI.SL.DanhMuc.DM_Table
{
    public partial class DM_Table_MainWindow: UserControl, Csla.Xaml.ISupportNavigation
    {

        #region ISupportNavigation Members
        List<Control> controls = new List<Control>();
        public bool CreateBookmarkAfterLoadCompleted
        {
            get { return true; }
        }

        public event EventHandler LoadCompleted;

        protected virtual void OnLoadCompleted()
        {
            if (LoadCompleted != null)
                LoadCompleted(this, EventArgs.Empty);
        }

        public void SetParameters(string parameters)
        {
            try
            {
                if (parameters == "")
                    Khoa = null;
                else
                    Khoa = Int32.Parse(parameters);
            }
            catch
            {
                Khoa = null;
            }
        }

        public string Title
        {
            get { return "Table"; }
        }

       
        #endregion

        #region Load Methods
        private System.Nullable<int> Khoa = null;
      
        public DM_Table_MainWindow(int Khoa)          
        {
            this.Khoa = Khoa;
        }

        public DM_Table_MainWindow()
        {
            LocalizationConverter converter = new LocalizationConverter();
            converter.LocalResourceManager = new ResourceManager("HeThongThongTin.UI.SL.DanhMuc.DM_Table.Resources", Assembly.GetExecutingAssembly());
            this.Resources.Add("Localization", converter);
            
            InitializeComponent();
            ControlBehavior.ControlBehaviors.Navigation(this.DM_Table_GeneralInformation_GroupBox, controls);
           
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            if (!System.ComponentModel.DesignerProperties.GetIsInDesignMode(this))
            {
                
                System.Windows.Data.CollectionViewSource DM_Table_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_Table_ViewModelSource"];
                if (Khoa == null)
                {
                    DM_Table_ViewModel DM_Table_ViewModel = new DM_Table_ViewModel();
                    DM_Table_ViewModel.ChildList_AddedNew += new DM_Table_ViewModel.ChildList_AddedNew_EventHandler(DM_Table_ViewModel_ChildList_AddedNew);
                    DM_Table_CollectionViewSource.Source = new List<object> { DM_Table_ViewModel };    
                }
                else
                {
                    DM_Table_ViewModel DM_Table_ViewModel = new DM_Table_ViewModel((int)Khoa);
                    DM_Table_ViewModel.ChildList_AddedNew += new DM_Table_ViewModel.ChildList_AddedNew_EventHandler(DM_Table_ViewModel_ChildList_AddedNew);
                    DM_Table_CollectionViewSource.Source = new List<object> { DM_Table_ViewModel };    
                }

               
                this.DM_Table_FieldGridView.KeyboardCommandProvider = new ControlBehavior.GridViewKeyboardCommandProvider(this.DM_Table_FieldGridView);   
            }
        }

        #endregion
       

        #region Event Handle
        private void LayoutRoot_KeyUp(object sender, KeyEventArgs e)
        {
            if (Keyboard.Modifiers == ModifierKeys.Control)
            {
                DM_Table_ViewModel vm = GetViewModel();
                if (e.Key == Key.S)
                {
                    if (vm.CanSave)
                    {
                        vm.Save(sender, new Csla.Xaml.ExecuteEventArgs());
                    }
                }
                else if (e.Key == Key.F6)
                {
                    AddItems();
                }
                else if (e.Key == Key.D1)
                {
                    this.DM_Table_FieldTab.IsSelected = true;
                }
                
            }
            e.Handled = true;
        }

        void AddItemButton_Click(object sender, RoutedEventArgs e)
        {

            AddItems();
        }

        void DeleteItemButton_Click(object sender, RoutedEventArgs e)
        {
            DeleteItems();
          
        }

        public void AddItems()
        {
            if (this.DM_Table_FieldTab.IsSelected)
            {
                AddFieldDetailsItem();
            }
            
        }
        private RadGridView GetActiveGridView()
        {
            if (this.DM_Table_FieldTab.IsSelected)
            {
                return this.DM_Table_FieldGridView;
            }
            
            return null;
        }

        void DeleteItems()
        {
            if (this.DM_Table_FieldTab.IsSelected)
            {
                DM_Table_FieldChild ChiTietChild = (DM_Table_FieldChild)(this.DM_Table_FieldGridView.SelectedItem);
                IList<DM_Table_FieldChild> list = this.GetViewModel().Model.DM_Table_Fields;
                int index = list.IndexOf(ChiTietChild);
                DeleteFieldDetailsItem(index);
            }  

        }
           
        void GridView_AddingNewDataItem(object sender, GridViewAddingNewEventArgs e)
        {
            var grid = e.OwnerGridViewItemsControl;
            if (grid.Items.Count > 0)
            {
                GridViewUtil.FocusRow((RadGridView)grid, grid.Items.Count - 1);
                Dispatcher.BeginInvoke(new Action(() =>
                {
                    grid.CurrentCellInfo = new GridViewCellInfo(grid.Items[grid.Items.Count - 1], grid.Columns[0]);
                    grid.Focus();
                    grid.BeginEdit();
                }));
            }
        }
              
        private void SaveAndNewButton_Click(object sender, RoutedEventArgs e)
        {
            DM_Table_ViewModel vm = GetViewModel();
            vm.SaveAndNew();
        }

        private void Control_KeyUp(object sender, KeyEventArgs e)
        {
            ControlBehavior.ControlBehaviors.Control_KeyUp(sender, e, controls);
        }


        private void FocusAfterChangeTab(GridViewDataControl grid, int index)
        {

            Dispatcher.BeginInvoke(new Action(() =>
            {
                grid.CurrentCellInfo = new GridViewCellInfo(grid.Items[index], grid.Columns[0]);
                grid.Focus();
                grid.BeginEdit();
            }));


        }

        #endregion

        #region View Model Related Methods
        private DM_Table_ViewModel GetViewModel()
        {
            System.Windows.Data.CollectionViewSource DM_Table_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_Table_ViewModelSource"];
            return ((List<object>)DM_Table_CollectionViewSource.Source)[0] as DM_Table_ViewModel;
        }

        

        private void AddFieldDetailsItem()
        {
            GetViewModel().AddDM_Table_FieldItem();
        }
        private void DeleteFieldDetailsItem(int Index)
        {
            GetViewModel().DeleteDM_Table_FieldItem( Index);
        }

        void DM_Table_ViewModel_ChildList_AddedNew(object sender, ChildListAddedNewArgs e)
        {
            if (e.ChildListName == "DM_Table_Fields")
            {
                this.DM_Table_FieldGridView.OnAddingNewDataItem(new GridViewAddingNewEventArgs(GetActiveGridView()));
            }
            
        }
        #endregion
        
    }
}
